package com.to.aboomy.bannersample.pagetransformer;

import com.to.aboomy.banner.BannerViewPager;
import com.to.aboomy.bannersample.util.NumberUtil;
import ohos.agp.components.Component;

/**
 * @author xujianhong
 * @since 2021-08-06
 */
public class ScaleInOutTransformer implements BannerViewPager.PageTransformer{
    @Override
    public void transformerPage(BannerViewPager.PageType pageType, Component page, float offset, float offsetPixels) {
        float scale = 0F;

        switch (pageType) {
            case PREVIOUS:
                offset = 1 - offset;
                if (offsetPixels < 0) {
                    page.setPivotX(page.getWidth());
                    page.setPivotY((float) page.getHeight() / 2.0F);
                    scale = NumberUtil.sub(1.0F, offset);
                    page.setScaleX(scale);
                    page.setScaleY(scale);
                }
                break;
            case CURRENT:
                if (offsetPixels >= 0) {
                    page.setPivotX(page.getWidth());
                    page.setPivotY((float) page.getHeight() / 2.0F);
                    scale = NumberUtil.sub(1.0F, offset);
                }else{
                    page.setPivotX(0);
                    page.setPivotY((float) page.getHeight() / 2.0F);
                    scale = NumberUtil.plus(1.0F, offset);
                }
                page.setScaleX(scale);
                page.setScaleY(scale);
                break;

            case NEXT:
                offset = offset - 1 ;
                if (offsetPixels >= 0) {
                    page.setPivotX(0);
                    page.setPivotY((float) page.getHeight() / 2.0F);
                    scale = NumberUtil.plus(1.0F, offset);
                    page.setScaleX(scale);
                    page.setScaleY(scale);
                } else {
                    page.setPivotX(0);
                    page.setPivotY((float) page.getHeight() / 2.0F);
                    scale = NumberUtil.plus(1.0F, offset);
                    page.setScaleX(scale);
                    page.setScaleY(scale);
                }
                break;

            case OTHER:
                page.setPivotX(1F);
                page.setPivotY(1F);
                page.setScaleX(1F);
                page.setScaleY(1F);
                break;
        }
    }
}
